package cn.lblbc.stopwatch

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class LapAdapter(private val lapRecords: List<LapRecord>) :
    RecyclerView.Adapter<LapAdapter.LapViewHolder>() {

    class LapViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val lapNumberText: TextView = view.findViewById(R.id.lapNumberText)
        private val lapTimeText: TextView = view.findViewById(R.id.lapTimeText)

        fun bind(lapRecord: LapRecord) {
            lapNumberText.text = String.format("%02d", lapRecord.number)
            lapTimeText.text = lapRecord.time
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LapViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_lap_record, parent, false)
        return LapViewHolder(view)
    }

    override fun onBindViewHolder(holder: LapViewHolder, position: Int) {
        holder.bind(lapRecords[position])
    }

    override fun getItemCount(): Int = lapRecords.size
}